package com.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ZhangYue26
 * @date 2020-07-02 18:03
 * @Description: 测试Controller
 */
@Slf4j
@Controller
public class DemoController {


    /**
     * @param []
     * @return java.lang.String
     * @Description 测试Controller
     * @author ZhangYue26
     * @date 2020/10/28
     **/
    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String test(HttpServletRequest request) {
        System.out.print(request.getHeader("user-agent") + "=======");
        System.out.println(request.getSession().getId());
        return "Success";
    }

    /**
     * @param [request, response]
     * @return void
     * @Description Excel文件导出
     * @author ZhangYue26
     * @date 2020/10/28
     **/
    @RequestMapping(value = "exportExcelFile.do", method = RequestMethod.GET)
    @ResponseBody
    public void excelFileExport(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");//xlsx
        response.setContentType("application/vnd.ms-excel;charset=utf-8");//xls
        response.addHeader("Content-Disposition", "attachment;fileName=ExportExcelFile.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        Map<String, String> fileMap = new HashMap() {{
            put("ssssssss", "1111111111");
            put("nnnnnnnn", "2222222222");
            put("mmmmmmmm", "3333333333");
            put("vvvvvvvv", "4444444444");
            put("tttttttt", "5555555555");
        }};
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(0);
        Cell cell0 = row.createCell(0);
        cell0.setCellType(CellType.STRING);
        cell0.setCellValue("ssssssss");
        Cell cell1 = row.createCell(1);
        cell1.setCellType(CellType.STRING);
        cell1.setCellValue("1111111111");
        Cell cell2 = row.createCell(2);
        cell2.setCellType(CellType.STRING);
        cell2.setCellValue("nnnnnnnn");

        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }


    /**
     * @param []
     * @return void
     * @Description 测试logback可用性
     * @author ZhangYue26
     * @date 2020/12/12
     **/
//    @Scheduled(fixedDelay = 10,initialDelay = 10)
    public void testLog() {
        log.info("info===" + System.currentTimeMillis());
        log.debug("debug===" + System.currentTimeMillis());
        log.error("error===" + System.currentTimeMillis());
        log.trace("trace===" + System.currentTimeMillis());
        log.warn("warn===" + System.currentTimeMillis());
    }

    @RequestMapping(path = "/scope", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public void getScope(HttpServletRequest request) {
        while (true) {
            System.out.println(request.getSession().getId());
        }
    }
}
